#include "e2d/core/render/LightSource.h"
#include "e2d/core/trans/TransformNode.h"
#include "e2d/core/Entity.h"

void e2d::LightSource::OnLoad()
{

}

void e2d::LightSource::UpdateLightEffect(fxcc::graph::common::LightEffect& l) const
{
	if (!m_Entity->m_Exist)
	{
		return;
	}

	const auto& transformNode = m_Entity->GetOrEmplace<e2d::TransformNode>();
	const auto& transformNodeWorld = transformNode.m_World;

	if (m_EffectData.IsDir() && l.hasLeftDirLight())
	{
		auto& buffer = l.dirlights[l.numDirlights].m_Buffer;
		buffer.ambient = m_EffectData.m_ColorAmbient;
		buffer.diffuse = m_EffectData.m_ColorDiffuse;
		buffer.specular= m_EffectData.m_ColorSpecular;
		buffer.direction = transformNodeWorld.GetForward();
		l.numDirlights++;
		
	}

	if (m_EffectData.IsSpot() && l.hasLeftSpotLight())
	{
		auto& buffer = l.spotLights[l.numSpotLights].m_Buffer;
		buffer.ambient = m_EffectData.m_ColorAmbient;
		buffer.diffuse = m_EffectData.m_ColorDiffuse;
		buffer.specular = m_EffectData.m_ColorSpecular;
		buffer.constant = m_EffectData.m_AttenuationConstant;
		buffer.linear = m_EffectData.m_AttenuationLinear;
		buffer.quadratic = m_EffectData.m_AttenuationQuadratic;
		buffer.cutOff = glm::cos(glm::radians(m_EffectData.m_AngleInnerCone));
		buffer.outerCutOff = glm::cos(glm::radians(m_EffectData.m_AngleOuterCone));

		buffer.direction = transformNodeWorld.GetForward();
		buffer.position = transformNodeWorld.m_Position;
		l.numSpotLights++;

	}

	if (m_EffectData.IsPoint() && l.hasLeftPointLight())
	{
		auto& buffer = l.pointLights[l.numPoints].m_Buffer;
		buffer.ambient = m_EffectData.m_ColorAmbient;
		buffer.diffuse = m_EffectData.m_ColorDiffuse;
		buffer.specular = m_EffectData.m_ColorSpecular;
		
		buffer.lightPbrColor = m_EffectData.m_PbrColor * m_EffectData.m_PbrLightStrength;
		buffer.position = transformNodeWorld.m_Position;
		l.numPoints++;

	}

}
